package cn.digitalize.generate.modular.basic.service.impl;

import cn.digitalize.generate.core.enums.GenEffectTypeEnum;
import cn.digitalize.generate.core.util.GenerateUtil;
import cn.digitalize.generate.modular.basic.entity.CodeGenerateBasic;
import cn.digitalize.generate.modular.basic.mapper.CodeGenerateBasicMapper;
import cn.digitalize.generate.modular.basic.result.MysqlColumnResult;
import cn.digitalize.generate.modular.basic.result.MysqlTableResult;
import cn.digitalize.generate.modular.basic.service.CodeGenerateBasicService;
import cn.digitalize.generate.modular.config.entity.CodeGenerateConfig;
import cn.digitalize.generate.modular.config.service.CodeGenerateConfigService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Created by ZhangCheng on 2023/06/09 (代码生成基础配置)
 */
@Service
public class CodeGenerateBasicServiceImpl extends ServiceImpl<CodeGenerateBasicMapper, CodeGenerateBasic> implements CodeGenerateBasicService {

    @Resource
    private CodeGenerateConfigService codeGenerateConfigService;

    @Override
    public List<MysqlColumnResult> queryMysqlColumnResult(String tableName) {
        return this.baseMapper.queryMysqlColumnResult(tableName);
    }

    @Override
    public void addGenerateBasic(CodeGenerateBasic codeGenerateBasic) {
        this.save(codeGenerateBasic);
        addGenerateConfig(codeGenerateBasic);
    }

    @Override
    public List<MysqlTableResult> queryMysqlTableResult() {
        return this.baseMapper.queryMysqlTableResult();
    }

    private void addGenerateConfig(CodeGenerateBasic codeGenerateBasic) {
        String tableName = codeGenerateBasic.getTableName();
        Long codeGenId = codeGenerateBasic.getId();
        List<MysqlColumnResult> columnResults = this.queryMysqlColumnResult(tableName);
        columnResults.forEach(column -> {
            String columnName = column.getColumnName();
            String dataType = column.getDataType();
            String isNullable = column.getIsNullable();
            //公用字段
            boolean isCommonField = GenerateUtil.isCommonField(columnName);
            String javaType = GenerateUtil.getJavaTypeBySqlType(dataType);
            String javaName = StrUtil.toCamelCase(columnName);
            CodeGenerateConfig config = new CodeGenerateConfig();
            config.setCodeGenId(codeGenId);
            config.setColumnName(columnName);
            config.setDataType(dataType);
            config.setJavaName(javaName);
            config.setJavaType(javaType);
            config.setColumnComment(column.getColumnComment());
            config.setEffectType(GenEffectTypeEnum.INPUT.getValue());
            if (isNullable.equals("YES")) {
                config.setWhetherRequired(false);
            } else {
                config.setWhetherRequired(true);
            }
            if (!isCommonField) {
                config.setWhetherTable(true);
                config.setWhetherAddUpdate(true);
                config.setWhetherCommon(false);
            } else {
                config.setWhetherTable(false);
                config.setWhetherAddUpdate(false);
                config.setWhetherCommon(true);
            }
            config.setSort(column.getOrdinalPosition());
            codeGenerateConfigService.save(config);
        });
    }
}
